*************** Castle and Pelc 2019: Leaks ISQ  *******************
************* All files and figures for publication ****************

** Date: March 2019
** Authors: Matthew Castle and Krzysztof Pelc
** Output: Regression tables and all figures for Castle and Pelc, "The Causes and Effects of Leaks in International Negotiations", ISQ.


**** SETUP
cd "~/Stata replication"
clear
capture log close
log using "CaPe ISQ replications.smcl", replace


************************************
*********** REGRESSIONS ************ 
************************************

use "Leaks ISQ regressions_replication_final.dta", clear


* Detrend variables for Table 1
regress mean_mean_rile year
predict rile_residual, resid

regress rile_polarize year
predict rile_polar_residual, resid

glm sum_leaks_EUonly year, family(poisson) link(logit)
predict EUleaks_resid_r, r

glm sum_leaks_EUonly year, family(poisson) link(logit)
predict EUleaks_resid_w, w

glm sum_leaks_possEU year, family(poisson) link(logit)
predict poss_EUleaks_resid_r, r

glm sum_leaks_possEU year, family(poisson) link(logit)
predict poss_EUleaks_resid_w, w

glm sum_leaks_notEU year, family(poisson) link(logit)
predict not_EUleaks_resid_r, r

glm sum_leaks_notEU year, family(poisson) link(logit)
predict not_EUleaks_resid_w, w


******* logit model, for predictive power
logit leaks_EUonly_dum mean_mean_rile rile_polarize, cluster(year)
estat class

// output shows that Right-Left party ideology and Right-Left polarization correctly classifies over 76% of leaks.


*********** Poisson regressions, make table 1

* Base model
poisson sum_leaks_EUonly   mean_mean_rile     , cluster(year)
est store cpt1
poisson sum_leaks_EUonly   mean_mean_rile  rile_polarize   , cluster(year)
est store cpt2

* control for time (splines)
poisson sum_leaks_EUonly   mean_mean_rile  rile_polarize  bs* , cluster(year)
est store cpt3

* control for time (residuals)
reg EUleaks_resid_r   rile_resid  rile_polar_resid    , cluster(year)
est store cpt4

* control for time (working residuals)
reg EUleaks_resid_w   rile_resid  rile_polar_resid    , cluster(year)
est store cpt5


** Label variables for table 1
label var mean_mean_rile  "Mean Right-Left"  
label var mean_mean_more_mktregulation  "Mean Pro-Market Regulation"  
label var rile_polarize   "Mean R-L Political Polarization"  
label var mktreg_polarize "Mean Market Regulation Polarization"

label var rile_resid  "Mean Right-Left (residuals)"  
label var rile_polar_resid   "Mean R-L Political Polarization (residuals)"
label var EUleaks_resid_r "EU-Attributed Leaks (residuals)"
label var EUleaks_resid_w "EU-Attributed Leaks (working residuals)"


esttab cpt1 cpt2 cpt3 cpt4 cpt5 using table1_07_21a.tex, replace cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) title(Leaks \label{leaks}) style(tex) compress legend varlabels(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) label stats(N r2 , fmt(0 2 3) label(N R-squared )) order()



** make table 1: Potential EU leaks
poisson sum_leaks_possEU   mean_mean_rile     , cluster(year)
est store cpt6
poisson sum_leaks_possEU   mean_mean_rile  rile_polarize   , cluster(year)
est store cpt7

*** control for time (splines)
poisson sum_leaks_possEU   mean_mean_rile  rile_polarize  bs* , cluster(year)
est store cpt8

* residuals 1
reg poss_EUleaks_resid_r   rile_resid  rile_polar_resid    , cluster(year)
est store cpt9

* residuals 2
reg poss_EUleaks_resid_w   rile_resid  rile_polar_resid    , cluster(year)
est store cpt10



esttab cpt6 cpt7 cpt8 cpt9 cpt10 using table1_07_21b.tex, replace cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) title(Leaks \label{leaks}) style(tex) compress legend varlabels(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) label stats(N r2 , fmt(0 2 3) label(N R-squared )) order()


** make table 1: Non-EU leaks
poisson sum_leaks_notEU   mean_mean_rile     , cluster(year)
est store cpt11
poisson sum_leaks_notEU   mean_mean_rile  rile_polarize   , cluster(year)
est store cpt12

*** control for time (splines)
poisson sum_leaks_not   mean_mean_rile  rile_polarize  bs* , cluster(year)
est store cpt13

* residuals 1
reg not_EUleaks_resid_r   rile_resid  rile_polar_resid    , cluster(year)
est store cpt14

* residuals 2
reg not_EUleaks_resid_w   rile_resid  rile_polar_resid    , cluster(year)
est store cpt15


esttab cpt11 cpt12 cpt13 cpt14 cpt15 using table1_07_21c.tex, replace cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) title(Leaks \label{leaks}) style(tex) compress legend varlabels(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) label stats(N r2 , fmt(0 2 3) label(N R-squared )) order()


********** margins to get substantive effect
su mean_mean_rile
di -3.694016  -  1.824414
* = -5.51843
poisson sum_leaks_EUonly   mean_mean_rile  rile_polarize   , cluster(year)
margins, atmeans
margins, at(mean_mean_rile=-5.51843)
di (.4797916 -   .2885039 )/  .2885039 
* .66303332
* a SD decrease in Rile, i.e. an increase in left-leaning parties, is associated with an increase in number of leaks of 66.3%

su rile_polarize
di   43.32214   + 3.006632 
* = 46.328772
poisson sum_leaks_EUonly   mean_mean_rile  rile_polarize   , cluster(year)
margins, atmeans
margins, at(rile_polarize= 46.328772)

di ( .4627171 -  .2885039 ) /  .2885039 
* .60385042
* a SD increase in polarization is associated with an increase in number of leaks of 60.3%



*********** Account for over-dispersion

sum sum_leaks_EUonly // variance (.6 squared = .36) > mean (.31)
hist sum_leaks_EUonly, discrete freq // many zeros

** Zero-inflated negative binomial model
** use proximity to negotiation round

use "leak_month_nego_rounds.dta", clear

* time
bspline , xvar(mdate) gen(bs) power(3)

* polarization
gen rile_polarize = abs(mean_min_rile-mean_max_rile)

*** do negotiation rounds predict leaks? 

foreach var in sum_nego_meetings sum_TTIP_nego sum_TPP_nego sum_CETA_nego {
	replace `var' = 0 if `var'==.
}

** all megaregional leaks
gen sum_leaks_megaregional = (sum_leaks_TTIP + sum_leaks_CETA + sum_leaks_TPP)

** non-megaregional leaks
gen non_megareg_leaks = (sum_leaks_possEU + sum_leaks_EUonly + sum_leaks_notEU) - sum_leaks_megaregional
replace non_megareg_leaks = . if year>2015


** CETA and TTIP leaks and negotiations
gen EU_mega_leaks = sum_leaks_TTIP + sum_leaks_CETA
gen sum_EU_mega_negos = sum_TTIP_nego + sum_CETA_nego

******** ZINB model

zinb sum_leaks_EUonly mean_mean_rile rile_polarize, inflate(sum_EU_mega_negos mean_mean_more_protectionism) vuong 
est save zinb, replace

estimates use zinb
estimates store zinb


* label variables

label var sum_leaks_EUonly "EU-Attributed Leaks"

label var mean_mean_rile  "Mean Right-Left"  
label var rile_polarize   "Mean R-L Political Polarization"  
label var sum_EU_mega_negos "Monthly CETA and TTIP negotiations"
label var mean_mean_more_protectionism  "Mean protectionist sentiment"  

esttab zinb using zinb_EU_7_21.tex, replace cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) title(ZINB model for EU CETA and TTIP leaks  \label{zinb_leaks}) style(tex) compress legend varlabels(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) label stats(N r2 , fmt(0 2 3) label(N R-squared )) order()



************************************
************* FIGURES **************
************************************

*** create leaks over time (Figure 1)
use "leaks and baseline by month.dta", clear

* Make Figure 1: leaks per month, 2006-2015
twoway (line quarterly_leaks mdate if year>2004, lpattern (longdash) lcolor(black) yax(2)) (lfitci quarterly_leaks mdate if year>2004, lcolor(black) alc(gs12) yax(2)), legend(size(small)) graphregion(color(white))
graph export "CaPe_Figure1.eps", as(eps) preview(off) replace

*** Main figures
use "Leaks_file_for_figures.dta", clear

* Create Leaks by negotiation (Figure 2)
set scheme s2mono

graph pie freq, over(Negotiation_n) sort desc legend(symysize(2) symxsize(5) cols(3) size(vsmall)) graphregion(color(white))
*save
graph export "CaPe_Figure2.eps", as(eps) preview(off) replace

* Pre- and Post-WTO (Figure 3)
set scheme s2mono

graph bar (count) Pre_WTO Post_WTO, ytitle("Number of leaks") legend(symysize(2) symxsize(5) cols(2) size(small) label(1 "Pre-WTO issues") label(2 "Post-WTO issues")) graphregion(color(white)) bargap(90) outergap(50)
*save
graph export "CaPe_Figure3.eps", as(eps) preview(off) replace

* Issue-areas (DESTA2 coding) (Figure 4)
set scheme s2mono
graph hbar (count) D2_ip D2_services D2_investment D2_market D2_tbt D2_sps D2_procurement D2_competition D2_defense D2_dispute D2_non_trade, blabel(name) ytitle("Number of leaks") legend(off) graphregion(color(white))
// manually edit bar labels and x-axis range
*save
//graph export "CaPe_Figure4.eps", as(eps) preview(off) replace



********** Leaks news coverage **********

use "CETA_coverage.dta", clear

* leak vs official
gen release = 1 if leak==0
replace release = 0 if leak==1

* statements
gen num_statements = (positivegovernmentstatement + positiveoppositionstatement + positivengostatement + negativegovernmentstatement + negativeoppositionstatement + negativengostatement)
gen num_positive = (positivegovernmentstatement + positiveoppositionstatement + positivengostatement)
gen num_negative = (negativegovernmentstatement + negativeoppositionstatement + negativengostatement)

* proportions
gen pos_prop = num_positive/num_statements if num_statements!=0

gen positive = 1 if pos_prop>.5 & num_statements!=0
replace positive = 0 if pos_prop<=.5 & num_statements!=0

gen neg_prop = num_negative/num_statements if num_statements!=0 

gen negative = 1 if neg_prop>.5 & num_statements!=0
replace negative = 0 if neg_prop<.5 & num_statements!=0


* is government more likely to make a positive statement? 
gen govt_pos = 1 if positivegovernmentstatement>0 & positivegovernmentstatement!=.
replace govt_pos = 0 if govt_pos==.

label variable govt_pos "Statements by government"
label define govt_pos 0 "No positive statement" 1 "Positive statement"
label values govt_pos govt_pos


label variable release "News coverage" 
label define release 0 "Leak coverage" 1 "Official release coverage"
label values release release

tab positivegovernmentstatement release, chi V
tab release govt_pos , exp chi V

* make Table 3

latab release govt_pos


capture log close


***************************************************************
****************************  END  ****************************
***************************************************************
